
package servlet;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.exceptions.NonexistentEntityException;


public class ServletUser extends HttpServlet {

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, NonexistentEntityException {
        String halaman = request.getParameter("halaman"); //variabel bantu untuk melakukan mapping halaman
        RequestDispatcher rd = null;
      
        if (halaman != null) {
          
            if (halaman.equals("sudahregistrasi")) {
                ServletUserHelper suh = new ServletUserHelper(request);
                rd = request.getRequestDispatcher(suh.signUp());
            } else if (halaman.equals("registrasi")) {
                rd = request.getRequestDispatcher("registrasi.jsp");
            } else if (halaman.equals("awal")) {
                rd = request.getRequestDispatcher("index.jsp");
            } else if (halaman.equals("daftar")) {
                ServletUserHelper suh = new ServletUserHelper(request);
                rd = request.getRequestDispatcher(suh.hapusUser());
            } else if (halaman.equals("edit")) {
                ServletUserHelper suh = new ServletUserHelper(request);
                rd = request.getRequestDispatcher(suh.bukaEditProfil());
            } else if (halaman.equals("sudahedit")) {
                ServletUserHelper suh = new ServletUserHelper(request);
                rd = request.getRequestDispatcher(suh.submitEditProfil());
            } else if (halaman.equals("loginprofil")) {
                ServletUserHelper suh = new ServletUserHelper(request);
                rd = request.getRequestDispatcher(suh.bukaLogin());
            } else if (halaman.equals("sudahlogin")) {
                ServletUserHelper suh = new ServletUserHelper(request);
                rd = request.getRequestDispatcher(suh.submitLogin());
            } else if (halaman.equals("logout")) {
                ServletUserHelper suh = new ServletUserHelper(request);
                rd = request.getRequestDispatcher(suh.logout());
            }
            else if (halaman.equals("tentangkami")) {
                ServletUserHelper suh = new ServletUserHelper(request);
                rd = request.getRequestDispatcher(suh.tentangKami());
            }
        } else {
            rd = request.getRequestDispatcher("index.jsp");
        }
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ServletUser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ServletUser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
